﻿using System.Transactions;
using SFile = System.IO;

/*
 
 * 2014-2-13：添加一个递归算法获取一个文件夹下所有的文件
 *      
 *                  20:17 已经做好
 *                  
 * 2014-2-13: 对文件的支持的方法就先写这么多，以后在补充也没事。
 * 
 */

namespace Ycg.File
{
    public static class FileManager
    {

        public static string ReadAllContents(string filePath)
        {
            return SFile.File.ReadAllText(filePath);
        }

        public static byte[] ReadAllBytes(string filePath)
        {
            return SFile.File.ReadAllBytes(filePath);
        }

        public static void GetFiles(string path, bool recursive, FileEventHandler fileEventHandler)
        {
            foreach (string fileName in SFile.Directory.GetFiles(path))
            {
                bool isCancle = false;
                fileEventHandler(path, fileName, ref isCancle);
                if (isCancle) return;
            }

            if (recursive)
            {
                foreach (string directoryName in SFile.Directory.GetDirectories(path))
                {
                    GetFiles(directoryName, true, fileEventHandler);
                }
            }
        }

        public static bool FileExists(string filePath)
        {
            return SFile.File.Exists(filePath);
        }

        public static bool DirectoryExists(string directoryPath)
        {
            return SFile.Directory.Exists(directoryPath);
        }

        public static void DeleteFile(string filePath)
        {
            SFile.File.Delete(filePath);
        }

        public static void DeleteDirectory(string directoryPath)
        {
            SFile.Directory.Delete(directoryPath);
        }

        public static void WriteAllContents(string filePath, string contents, bool isUseTransaction = false)
        {
            if (isUseTransaction)
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    SFile.File.WriteAllText(filePath, contents);
                    transactionScope.Complete();
                }
            }
            else
            {
                SFile.File.WriteAllText(filePath, contents);
            }
        }

        public static void WriteAllBytes(string filePath, byte[] contents, bool isUseTransaction = false)
        {
            if (isUseTransaction)
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    SFile.File.WriteAllBytes(filePath, contents);
                    transactionScope.Complete();
                }
            }
            else
            {
                SFile.File.WriteAllBytes(filePath, contents);
            }
        }

        public static void AppendAllContents(string filePath, string contents, bool isUseTransaction = false)
        {
            if (isUseTransaction)
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    SFile.File.AppendAllText(filePath, contents);
                    transactionScope.Complete();
                }
            }
            else
            {
                SFile.File.AppendAllText(filePath, contents);
            }
        }
    }
}
